﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;

namespace DAO
{
    /// <summary>
    /// Summary description for Message
    /// </summary>
    public class Message
    {
        DBConnection DBConnection;
        public Message()
        {
            DBConnection = new DBConnection();
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Get all DTO.Message instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.Message instances.</returns>
        public List<DTO.Message> GetAllMessages()
        {
            Log.LogInfo("Begin GetAllMessages()");
            String query = "SELECT * FROM [Message];";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            List<DTO.Message> messageDTOs = new List<DTO.Message>();
            try
            {
                while (reader.Read())
                {
                    messageDTOs.Add(new DTO.Message(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetAllMessages(): " + ex.StackTrace);
            }
            DBConnection.Close();
            Log.LogInfo("End GetAllMessages()");
            return messageDTOs;
        }
        /// <summary>
        /// Get DTO.Message instance from DB.
        /// </summary>
        /// <param name="ID">Message ID</param>
        /// <returns>DTO.Message instance which has the specific ID.</returns>
        public DTO.Message GetMessage(String ID)
        {
            Log.LogInfo("Begin GetMessage(String ID)");
            String query = "SELECT * FROM [Message] WHERE [Message].[ID]='" + ID + "';";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            DTO.Message messageDTO = null;
            try
            {
                if (reader.Read())
                    messageDTO = new DTO.Message(reader);
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetMessage(String ID): " + ex.StackTrace);
            }

            Log.LogInfo("End GetMessage(String ID)");
            DBConnection.Close();
            return messageDTO;
        }

        /// <summary>
        /// Insert Message into database.
        /// </summary>
        /// <param name="Message">DTO.Message instance to insert into database.</param>
        public int InsertMessage(DTO.Message Message)
        {
            Log.LogInfo("Begin InsertMessage()");
            String query = "";
            query += "INSERT INTO MESSAGE ([From],[To],Time, Content) VALUES ('" + Message.From + "','" + Message.To + "','" + Message.Time + "','" + Message.Content + "')";
            query += "";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertMessage()");
            return result;
        }

        /// <summary>
        /// Update Message info into database.
        /// </summary>s
        /// <param name="Message">Message instance to be updated.</param>
        public int UpdateMessage(DTO.Message Message)
        {
            Log.LogInfo("Begin UpdateMessage()");
            String query = "UPDATE MESSAGE SET [From] = '" + Message.From + "'," + " [To] = '" + Message.To + "'Time ='" + Message.Time + "'Content = '" + Message.Content + "' WHERE ID = '" + Message.ID + "'";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertMessage()");
            return result;
        }
        /// <summary>
        /// Delete a specific Message from DB.
        /// </summary>
        /// <param name="Message">The Message to be deleted.</param>
        public int DeleteMessage(DTO.Message Message)
        {
            Log.LogInfo("Begin DeleteMessage()");
            String query = "DELETE FROM [Message] WHERE [Message].[ID]='" + Message.ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteMessage()");
            return result;
        }

        /// <summary>
        /// Delete a Message in database using ID.
        /// </summary>
        /// <param name="ID">Message ID.</param>
        public int DeleteMessage(String ID)
        {
            Log.LogInfo("Begin DeleteMessage()");
            String query = "DELETE FROM [Message] WHERE [Message].[ID]='" + ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteMessage()");
            return result;
        }
    }
}